home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / modules / loadilbm.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  6KB  |  221 lines

  1. /* loadilbm.c - C. Scheppner CBM
  2.  *
  3.  * High-level ILBM load routines
  4.  *
  5.  * 37.9  04/92 - use vp->ColorMap.Count instead of MAXAMCOLORREG
  6.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  7.  *            for future compatibility
  8.  * 39.1  07/92 - add support for V39 color loading functions
  9.  * 39.10 08/93 - fix queryilbm to only stop on a BODY, not on CMAP or BODY
  10.  *               (CAMG may be after CMAP)
  11.  */
  12. #define INTUI_V36_NAMES_ONLY
  13.  
  14. #include "iffp/ilbm.h"
  15. #include "iffp/ilbmapp.h"
  16.  
  17. extern struct Library *GfxBase;
  18.  
  19. /* loadbrush
  20.  *
  21.  * Passed an initialized ILBMInfo with a not-in-use ParseInfo.iff
  22.  *   IFFHandle and desired propchks, collectchks, and stopchks, and filename,
  23.  *   will load an ILBM as a brush, setting up ilbm->Bmhd, ilbm->camg,
  24.  *   ilbm->brbitmap, ilbm->colortable, and ilbm->ncolors
  25.  *
  26.  *   Note that ncolors may be more colors than you can LoadRGB4.
  27.  *   Use MIN(ilbm->ncolors,vp->ColorMap->Count) for color count if
  28.  *   you change the colors yourself using 1.3/2.0 functions.
  29.  *
  30.  * V39 - unless ilbm->IFFPFlags & IFFPF_NOCOLOR32, will do 32-bit
  31.  *   color load under V39 and higher
  32.  *
  33.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  34.  */
  35.  
  36. LONG loadbrush(struct ILBMInfo *ilbm, UBYTE *filename)
  37. {
  38. LONG error = 0L;
  39.  
  40.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  41.  
  42.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  43.     {
  44.     error = parseifile((struct ParseInfo *)ilbm,
  45.                 ID_FORM, ID_ILBM,
  46.                 ilbm->ParseInfo.propchks,
  47.                 ilbm->ParseInfo.collectchks,
  48.                 ilbm->ParseInfo.stopchks);
  49.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  50.         {
  51.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  52.         {
  53.             if(error = createbrush(ilbm))   deletebrush(ilbm);
  54.         }
  55.         else
  56.         {
  57.         message(SI(MSG_ILBM_NOILBM));
  58.         error = NOFILE;
  59.         }
  60.         }
  61.     if(error)    closeifile((struct ParseInfo *)ilbm);
  62.     }
  63.     return(error);
  64. }
  65.  
  66.  
  67. /* unloadbrush
  68.  *
  69.  * frees and close everything alloc'd/opened by loadbrush
  70.  */
  71. void unloadbrush(struct ILBMInfo *ilbm)
  72. {
  73.     deletebrush(ilbm);
  74.     closeifile((struct ParseInfo *)ilbm);
  75. }
  76.  
  77.  
  78. /* queryilbm
  79.  *
  80.  * Passed an initilized ILBMInfo with a not-in-use IFFHandle,
  81.  *   and a filename,
  82.  *   will open an ILBM, fill in ilbm->camg and ilbm->bmhd,
  83.  *   and close the ILBM.
  84.  *
  85.  * This allows you to determine if the ILBM is a size and
  86.  *   type you want to deal with.
  87.  *
  88.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  89.  */
  90.  
  91. /* query just wants these chunks */
  92. LONG queryprops[] = { ID_ILBM, ID_BMHD,
  93.               ID_ILBM, ID_CAMG,
  94.                       TAG_DONE };
  95.  
  96. /* scan can stop when a BODY is reached */
  97. LONG querystops[] = { ID_ILBM, ID_BODY,
  98.               TAG_DONE };
  99.  
  100. LONG queryilbm(struct ILBMInfo *ilbm, UBYTE *filename)
  101. {
  102. LONG error = 0L;
  103. BitMapHeader *bmhd;
  104.  
  105.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  106.  
  107.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  108.     {
  109.     D(bug("queryilbm: openifile successful\n"));
  110.  
  111.     error = parseifile((struct ParseInfo *)ilbm,
  112.             ID_FORM, ID_ILBM,
  113.             queryprops, NULL, querystops);
  114.  
  115.     D(bug("queryilbm: after parseifile, error = %ld\n",error));
  116.  
  117.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  118.         {
  119.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  120.         {
  121.         if(bmhd = (BitMapHeader*)
  122.             findpropdata(ilbm->ParseInfo.iff,ID_ILBM,ID_BMHD))
  123.             {
  124.             *(&ilbm->Bmhd) = *bmhd;
  125.             ilbm->camg = getcamg(ilbm);
  126.             }
  127.         else error = NOFILE;
  128.         }
  129.         else
  130.         {
  131.         message(SI(MSG_ILBM_NOILBM));
  132.         error = NOFILE;
  133.         }
  134.         }
  135.     closeifile(ilbm);
  136.     }
  137.     return(error);
  138. }
  139.  
  140.  
  141. /* loadilbm
  142.  *
  143.  * Passed a not-in-use IFFHandle, an initialized ILBMInfo, and filename,
  144.  *   will load an ILBM into your already opened ilbm->scr, setting up
  145.  *   ilbm->Bmhd, ilbm->camg, ilbm->colortable, and ilbm->ncolors
  146.  *   and loading the colors into the screen's viewport
  147.  *
  148.  *   Note that ncolors may be more colors than you can LoadRGB4.
  149.  *   Use MIN(ilbm->ncolors,vp->ColorMap->Count) for color count if
  150.  *   you change the colors yourself using 1.3/2.0 functions.
  151.  *
  152.  * V39 - unless ilbm->IFFPFlags & IFFPF_NOCOLOR32, will do 32-bit
  153.  *   color load under V39 and higher
  154.  *
  155.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  156.  *
  157.  * NOTE - loadilbm() keeps the IFFHandle open so you can copy
  158.  *   or examine other chunks.  You must call closeifile(iff,ilbm)
  159.  *   to close the file and deallocate the parsed context
  160.  *
  161.  */
  162.  
  163. LONG loadilbm(struct ILBMInfo *ilbm, UBYTE *filename)
  164. {
  165. LONG error = 0L;
  166.  
  167.  
  168.     D(bug("loadilbm:\n"));
  169.  
  170.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  171.     if(!ilbm->scr)        return(CLIENT_ERROR);
  172.     if(!(ilbm->vp))        ilbm->vp = &ilbm->scr->ViewPort;  /* 39.1 */
  173.  
  174.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  175.     {
  176.     D(bug("loadilbm: openifile successful\n"));
  177.  
  178.     error = parseifile((struct ParseInfo *)ilbm,
  179.             ID_FORM, ID_ILBM,
  180.             ilbm->ParseInfo.propchks,
  181.             ilbm->ParseInfo.collectchks,
  182.             ilbm->ParseInfo.stopchks);
  183.  
  184.     D(bug("loadilbm: after parseifile, error = %ld\n",error));
  185.  
  186.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  187.         {
  188.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  189.         {
  190.             error = loadbody(ilbm->ParseInfo.iff,
  191.                 ilbm->scr->RastPort.BitMap, &ilbm->Bmhd);
  192.  
  193.         D(bug("loadilbm: after loadbody, error = %ld\n",error));
  194.  
  195.         if(!error)
  196.             {
  197.             if(!(getcolors(ilbm)))    setcolors(ilbm,ilbm->vp);
  198.             }
  199.         }
  200.         else
  201.         {
  202.         message(SI(MSG_ILBM_NOILBM));
  203.         error = NOFILE;
  204.         }
  205.         }
  206.     if(error)    closeifile((struct ParseInfo *)ilbm);
  207.     }
  208.     return(error);
  209. }
  210.  
  211.  
  212. /* unloadilbm
  213.  *
  214.  * frees and closes everything allocated by loadilbm
  215.  */
  216. void unloadilbm(struct ILBMInfo *ilbm)
  217. {
  218.     closeifile((struct ParseInfo *)ilbm);
  219.     freecolors(ilbm);
  220. }
  221.